package com.cms.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.base.Pagination;
import com.cms.dao.ArticleDao;
import com.cms.dao.ChannelArticleMapperDao;
import com.cms.dao.ChannelDao;
import com.cms.entity.Article;
import com.cms.entity.Channel;
import com.cms.entity.ChannelArticleMapper;
import com.cms.service.ArticleService;


@Service(value="articleService")
public class ArticleServiceImpl implements ArticleService {
	
	private ArticleDao articleDao;
	
	private ChannelArticleMapperDao channelArticleMapperDao;
	
	private ChannelDao channelDao;

	
	
	public Article get(Serializable id) {
		return articleDao.find(id, Article.class);
	}

	public void delete(Serializable id) {
		articleDao.delete(id, Article.class);
	}

	public void save(Article entity) {
		articleDao.save(entity);
	}
	
	public void save(Article article, List<Serializable> channelIds){
		articleDao.save(article);
		for(int i=0; i<channelIds.size(); i++){
			int id = Integer.parseInt(channelIds.get(i).toString());
			ChannelArticleMapper cam = new ChannelArticleMapper();
			cam.setArticle(article);
			cam.setChannel(channelDao.find(id, Channel.class));
			channelArticleMapperDao.save(cam);
		}
		
	}

	public void update(Article entity) {
		articleDao.update(entity);
	}

	public Pagination<Article> queryArticles(Article article, int firstResult, int maxResults) {
		return articleDao.queryArticles(article, firstResult, maxResults);
	}
	
	public List<Article> queryChannelArticles(int channelId, int articleCount){
		return articleDao.queryChannelArticles(channelId, articleCount);
	}

	public List<Article> queryHeadlineArticles(int headlineCount){
		return articleDao.queryHeadlineArticles(headlineCount);
	}
	
	public List<Article> queryRecommendArticles(int recommendCount){
		return articleDao.queryRecommendArticles(recommendCount);
	}
	
	
	public ArticleDao getArticleDao() {
		return articleDao;
	}
	@Autowired
	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public ChannelArticleMapperDao getChannelArticleMapperDao() {
		return channelArticleMapperDao;
	}
	@Autowired
	public void setChannelArticleMapperDao(
			ChannelArticleMapperDao channelArticleMapperDao) {
		this.channelArticleMapperDao = channelArticleMapperDao;
	}

	public ChannelDao getChannelDao() {
		return channelDao;
	}
	@Autowired
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}
	
	
}
